<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>字典信息</title>
<%@ include file="/WEB-INF/jsp/include.jsp"%>
  <script type="text/javascript">
    var path = "${path}";
    var params=new Object();
	var url=path+"/system/dict/queryByPage";
	
   $(function() {
	   var grid=$("#tt");
		initGrid(grid);
		getTree();
	});

    //初始化表格和数据
   function initGrid(grid) {
	    var columns= [
	   	    { field: "dictId", title: "代码", align: "left",sortable: false,hidden:true, resizable: true, width: 80  }, 
	        { field: "dictCode", title: "编码", align: "left", sortable: false, resizable: true, width: 80  }, 
	        { field: "dictName", title:"名称",align: "left", sortable: false, resizable: true, width: 150},
	        { field: "dictType", title:"类型",align: "left", sortable: false, resizable: true, width: 150},
	        { field: "dictDescription", title:"描述",align: "left", sortable: false, resizable: true, width: 200},
	        { field: "sortby", title:"排序",align: "left", sortable: false, resizable: true, width: 80},
	     ];
	      initPageGrid(grid, params, url,columns,"#tb");
       //getPage(grid,params,url);

       var grid_options = $("#tt").datagrid("options");
       grid_options.onDblClickRow = function(rowIndex, rowData) {
           btnEditNodeByGrid();
       }
	   
	}
   
	//查询按钮
	function btnQuery(){
		params.dictName = $("#dictName").val();
		var grid=$("#tt");
		params.parentDictId="";
		params.pageNumber=1;
		getPage(grid,params,url);
	}
	
	//显示树
	function getTree(){
	   $.ajax({
     		type:"POST",
    		url:path+"/system/dict/getTree", 
    		dataType:"json",
    		success:function callback(data){
    			var analyMsg = data;
	   			if(analyMsg.code!="0000"){ 
  					$.messager.alert('操作提示',analyMsg.msg,'info',function(){});
	   				//$.alert("default", "error", analyMsg.msg);
	   			}else{ 					   			
			   		$.fn.zTree.init($("#treeDemo"), setting, analyMsg.returnObject.data);
			   		//默认第一个节点选中
			   		var zTree = $.fn.zTree.getZTreeObj("treeDemo"); 
	                var node = zTree.getNodeByParam('id', 0);
	                zTree.selectNode(node);
	                zTree.setting.callback.onClick(null, null, node);
				} 
    		}
   	   }); 
	}

	var setting = {
			callback: {
				onClick: zTreeOnClick
				
			},
			data: {
				key: {
					name: "name",
					title: ""
				},					
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pId",
					rootPId: "0"						
				}
			}
		}; 

	//单击父节点显示所包含的子节点信息
	function zTreeOnClick(event, treeId, treeNode) {
	    var id=treeNode.id;
	    params.parentDictId = id;
	    params.dictName = $("#dictName").val();
	    var grid=$("#tt");
	   params.pageNumber=1;
        //getTree();
		getPage(grid,params,url);
	}
	
	//增加
	 function btnAddNode() {
			layer.open({
				  type: 2,
                  title: ['字典信息 - 添加','background: #38acff;'],
				  shadeClose: false,
				  shade: 0.3,
				  area: ['560px', '230px'],
				  content: '${path}/system/dict/toAdd' //iframe的url
				}); 
	}
	 
	//修改
	 function btnEditNodeByGrid(){
			var rows = $('#tt').datagrid('getSelections');
		    if (rows.length == 0) {
		        $.messager.alert("操作提示", "请选择一条数据！", "info");
		        //$.alert("pick", "info", "字典");
		        return;
		    }
		    layer.open({
				  type: 2,
                  title: ['字典信息 - 修改','background: #38acff;'],
				  shadeClose: true,
				  shade: 0.3,
				  area: ['560px', '230px'],
				  content: '${path}/system/dict/toUpdate' //iframe的url
				}); 

   	}
     //修改，删除过后，刷新表格数据
	 function refreshGrid() {
         var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
         var nodes = treeObj.getSelectedNodes();
         params.parentDictId = nodes[0].id;
         var grid=$("#tt");
		   params.pageNumber=1;
			getPage(grid,params,url);
		    
		}
	 
     //逻辑删除，父节点删除，子节点跟着删除
	 function btnDel(id) {
	     var rows = $('#tt').datagrid('getSelected');
		 if (rows == null) {
		      $.messager.alert("操作提示", "请选择一条数据！", "info");
		      //$.alert("pick", "info", "字典");
		      return;
		  }
         $.messager.alert("问题", "请确认是否要删除？","confirm", function() {

                 //移除节点
                 var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                 var nodes= treeObj.getNodesByParam("id",rows.dictId,null);
                 treeObj.removeNode(nodes[0]);

                 var url = path+'/system/dict/delete';
                 var param = new Object();
                 param.dictId = rows.dictId;
                 param.dictType = rows.dictType;
                 param.dictCode = rows.dictCode;
                 $.ajax({
                     type:"POST",
                     url:url,
                     data:param,
                     dataType:"json",
                     async: true,
                     success:function(data){
                         if(data.code != '0000'){

                             $.messager.alert("操作提示", "删除数据时发生错误", "info");
                             //$.alert("delete", "error", "字典信息");
                         } else {
                             refreshGrid();
                         }
                     },
                     error: function() {
                         $.messager.alert("操作提示", "删除数据时发生错误", "info");
                         //$.alert("delete", "error", "字典信息");
                     }
                 });

         });
		
	}
  
    //新增信息后，刷新表格，显示新增数据
	function addRefreshGrid() {
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = treeObj.getSelectedNodes(); 
		params.parentDictId = nodes[0].id;    
		var grid=$("#tt");
		params.pageNumber=1;
	    getPage(grid,params,url);
	}
  </script>
</head> 

<body style="margin:0px; padding:0px;">
<%@ include file="/WEB-INF/jsp/loading.jsp" %>
  	<div class="easyui-layout" fit="true" >
		<div region="center" style="padding: 5px;"  border="false">
			<div class="easyui-layout" fit="true"  border="false">
				<div id="divNorth" region="north" title="查询条件" border="true" noheader="false" style="padding: 5px; height: 70px; background: none repeat scroll 0 0 #EFEFEF;">		
				   <table>
			        	<tr>
			        		<td>名称：</td><td><input type="text" id="dictName" name="dictName" style="width: 200px" /></td>
			        		<td>
								<input class="search-btn-blue" style="margin-left:10px" type="button" onclick="btnQuery();" value="查询">
							</td>	
			            </tr>
			        </table>			
				</div> 
				<div region="center" title="" border="false" noheader="true" style="padding: 5px 0 0 0;">
					<div class="easyui-layout" fit="true"  border="false">
			    		<div region="west" title="" border="true" noheader="true" split="true" style="width: 250px;">
			    			<div class="easyui-layout" fit="true"  border="false">
								<div region="center" title="字典信息树" border="false">  		
								    <div>		    		
					    				<ul id="treeDemo" class="ztree" style="width: 90%; height: 90%;"></ul>
					    			</div>
					    		</div>
			    			</div>
			    		</div>			 	
					    <div region="center" title="下级列表信息" border="true">
					        <table id="tt" title="" border="false"></table>
					    </div>
				    </div>
			    </div>
			</div>
	    </div>
    </div>
    <div id="tb" style="padding: 1px; height: auto; display: none;">
        <div>
        	<a id="btn_addNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false" onclick="btnAddNode()">新增</a>
			<a id="btn_editNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="false" onclick="btnEditNodeByGrid()">修改</a>
			<a id="btn_delNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="false" onclick="btnDel()">删除</a>
        </div>
    </div>
</body>
</html>
